package cn.frh.user.service.impl;

import cn.frh.basic.Exception.BusinessException;
import cn.frh.basic.util.Md5Utils;
import cn.frh.basic.util.StrUtils;
import cn.frh.user.domain.Logininfo;
import cn.frh.user.domain.User;
import cn.frh.user.dto.UserDto;
import cn.frh.user.mapper.LogininfoMapper;
import cn.frh.user.mapper.UserMapper;
import cn.frh.user.service.IUserService;
import cn.frh.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 业务实现类：
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LogininfoMapper logininfoMapper;


    @Override
    public User loadByPhone(String phone) {
        return userMapper.loadByPhone(phone);
    }

    @Override
    public void registerPhone(UserDto userDto) {
        String phone = userDto.getPhone();
        String phoneCode = userDto.getPhoneCode();
        String pwd = userDto.getPwd();
        String rePwd = userDto.getPwd();
        //1.1判断参数是否为空
        if (StringUtils.isEmpty(phone)
                ||StringUtils.isEmpty(phoneCode)
                ||StringUtils.isEmpty(pwd)
                ||StringUtils.isEmpty(rePwd)){
            throw new BusinessException("请填写必要数据");
        }
        //1.2判断密码是否相同
        if (!pwd.equals(rePwd)){
            throw new BusinessException("两次密码不一样 请重新输入");
        }
        //1.3判断验证码是否过期
        Object o = redisTemplate.opsForValue().get("register:" + phone);
        if (o == null){
            throw new BusinessException("验证码已过期 请重新获取");
        }
        //1.3判断验证码是否正确
        if (!o.toString().split(":")[0].equals(phoneCode)){
            throw new BusinessException("验证码不正确 请重新输入或获取");
        }

        //2添加到数据库
        User user = userDto2User(userDto);
        Logininfo logininfo = user2Logininfo(user);
        logininfoMapper.save(logininfo);
        user.setLogininfoId(logininfo.getId());
        userMapper.save(user);
    }

    private User userDto2User(UserDto userDto) {
        User user = new User();
        user.setUsername(userDto.getPhone());
        user.setPhone(userDto.getPhone());
        user.setState(1);
        //生成盐值
        String salt = StrUtils.getComplexRandomString(32);
        //加密加盐
        String md5 = Md5Utils.encrypByMd5(userDto.getPwd() + salt);
        user.setSalt(salt);
        user.setPassword(md5);
        return user;
    }
    private Logininfo user2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        //用工具类拷贝属性（同名原则）
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setType(1);
        logininfo.setDisable(true);
        return logininfo;
    }


    @Override
    public void save(User user) {
        //处理密码加密加盐
        //设置盐值
        String salt = StrUtils.getComplexRandomString(32);
        //加密加盐
        String md5 = Md5Utils.encrypByMd5(user.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5);

        //保存logininfo
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setType(1);
        logininfo.setDisable(true);
        user.setLogininfoId(logininfo.getId());
        super.save(user);
    }

    @Override
    public void remove(Long id) {
        User user = userMapper.loadById(id);
        logininfoMapper.remove(user.getLogininfoId());
        super.remove(id);
    }

    @Override
    public void update(User user) {
        String salt = StrUtils.getComplexRandomString(32);
        String md5 = Md5Utils.encrypByMd5(user.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5);
        super.update(user);

        //获取登录信息
        Long logininfoId = userMapper.loadById(user.getId()).getLogininfoId();
        Logininfo logininfo = logininfoMapper.loadById(logininfoId);
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setId(logininfoId);
        logininfoMapper.update(logininfo);
    }






}
